package com.yxj.threadcase1;

public class MyRunnable10 implements Runnable{
    int ticket = 0;
    @Override
    public void run() {
        while(true) {
            //同步代码块
            if(method()) break;
        }
    }
    private synchronized boolean method() {
        if(ticket == 100) {
            return true;
        }else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ticket++;
            System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
        }
        return false;
    }
}
